Page({
  data: {
    realName: '',
    idCard: '',
    frontImage: '',
    backImage: ''
  },

  // 上传图片
  uploadImage(e) {
    const type = e.currentTarget.dataset.type
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['camera', 'album'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0]
        wx.showLoading({ title: '上传中' })
        
        // 上传图片到服务器
        wx.uploadFile({
          url: 'your-api-url/upload/idcard',
          filePath: tempFilePath,
          name: 'image',
          success: (uploadRes) => {
            const data = JSON.parse(uploadRes.data)
            this.setData({
              [type === 'front' ? 'frontImage' : 'backImage']: data.url
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      }
    })
  },

  // 提交认证
  async submitVerify() {
    if (!this.validateForm()) {
      return
    }

    wx.showLoading({ title: '提交中' })
    try {
      const res = await wx.request({
        url: 'your-api-url/verify/submit',
        method: 'POST',
        data: {
          realName: this.data.realName,
          idCard: this.data.idCard,
          frontImage: this.data.frontImage,
          backImage: this.data.backImage
        }
      })

      if (res.data.success) {
        wx.showToast({
          title: '认证成功',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }
    } catch (error) {
      wx.showToast({
        title: '认证失败',
        icon: 'none'
      })
    }
  },

  // 表单验证
  validateForm() {
    if (!this.data.realName) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none'
      })
      return false
    }
    if (!this.data.idCard) {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      })
      return false
    }
    if (!this.data.frontImage || !this.data.backImage) {
      wx.showToast({
        title: '请上传身份证照片',
        icon: 'none'
      })
      return false
    }
    return true
  }
}) 